package com.lxr0860.lxr0860production_test.itemactivity;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;

import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.adapter.PhotoAdapter;
import com.lxr0860.lxr0860production_test.databinding.ActivityPhotoReviewBinding;
import com.lxr0860.lxr0860production_test.utils.Contants;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class PhotoReviewAcitivty extends Activity{

    private GridView mGvPhotoLayout;
    private List<String> photonames;
    private PhotoAdapter mAdapter;
    private Button mBtnSave;
    public static final int REQUEST_PHOTO=200;
    public static final String RESULT_KEY="photoPath";
    private int mCameraId;

    private ActivityPhotoReviewBinding photoReviewBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        photoReviewBinding = ActivityPhotoReviewBinding.inflate(getLayoutInflater());
        setContentView(photoReviewBinding.getRoot());
        initView();
        initData();
        initEvent();
    }

    private void initView(){
        mGvPhotoLayout= (GridView) findViewById(R.id.gv_photo_layout);
    }

    private void initData(){
        mCameraId = getIntent().getIntExtra("cameraId", Camera.CameraInfo.CAMERA_FACING_BACK);
        photonames=new ArrayList<String>();
        mAdapter=new PhotoAdapter(this,photonames);
        mGvPhotoLayout.setAdapter(mAdapter);
        mBtnSave= (Button) findViewById(R.id.btn_photo_result);
    }

    private void initEvent(){
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mIntent=new Intent();
                Bundle bundle=new Bundle();
                bundle.putBoolean("result",true);
                mIntent.putExtras(bundle);
                if(mCameraId== Camera.CameraInfo.CAMERA_FACING_BACK){
                    PhotoReviewAcitivty.this.setResult(Contants.TEST_ITEM_CAMERA,mIntent);
                }else {
                    PhotoReviewAcitivty.this.setResult(Contants.TEST_FONT_CAMERA,mIntent);
                }
                finish();
            }
        });
        Intent intent=new Intent(this,Camera2Activity.class);
        intent.putExtra("cameraId",mCameraId);
        startActivityForResult(intent,REQUEST_PHOTO);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUEST_PHOTO){
            if(resultCode==101){
                if(data!=null){
                    if(photonames!=null){
                        photonames.clear();
                        ArrayList<String> photos = data
                                .getStringArrayListExtra(PhotoReviewAcitivty.RESULT_KEY);
                        if(photos!=null&&photos.size()>0){
                            photonames.addAll(photos);
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }else {
                finish();
            }
        }
    }
}
